package cn.tedu.sp04.service;

import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.pojo.Order;
import cn.tedu.sp01.pojo.User;
import cn.tedu.sp01.service.OrderService;
import cn.tedu.sp04.feign.ItemClient;
import cn.tedu.sp04.feign.UserClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 张琪
 * @date 2020/12/18
 * @describe
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private ItemClient itemClient;

    @Autowired
    private UserClient userClient;

    @Override
    public Order getOrder(String orderId) {
//        假的订单数据
        log.info("获取订单:, orderId=" + orderId);
        Order order = new Order();
        order.setId(orderId);
//        TODO: 远程调用用户, 获取用户信息, 这里用本地创建的代替
        order.setUser(userClient.getUser(7).getData());
//        order.setUser(new User());
//        TODO: 远程调用商品, 获取商品列表, 这里用本地创建的代替
        order.setItems(itemClient.getItems(orderId).getData());
//        order.setItems(new ArrayList<Item>());

        return order;
    }

    @Override
    public void addOrder(Order order) {
        log.info("添加订单: " + order);
//        TODO: 远程调用商品, 减少商品库存
        itemClient.decreaseNumber(order.getItems());
//        TODO: 远程调用用户, 增加用户积分
        userClient.addScore(order.getUser().getId(), 1000);
    }
}
